%GENERATE TABLE 3;
clear all; addpath('auxiliar'); addpath('Simulated_Datasets'); load SIMULATION_ERGODIC_DE.mat;
momsDE=moms;
mbindDE=mean(bindSIM)*100;
mSSDE=mean(SS)*100;
mDefDE= mean(DefSIM)*100;
load SP_SIMULATION_20k


VSP=V;
bgrid_SP=bgrid_large;
QSP=eQ_temp_large;

bpSP=bp;
moments_inorder=[mean(NFA),avPvD,avPub,avSpr,std(NFA),a_Std_PvDebt,a_Std_PubDebt,a_Std_Spr];
moms=[avSpr,a_Std_Spr,avPvD,avPub, a_Std_PvDebt, a_Std_PubDebt,mean(NFA),std(NFA)];
 clear bpSIM VSIM bSIM V BGpSIM BGSIM S_index BGiSIM bgrid Q bp
 
 load simDE.mat
 
[T,~]=size(VSIM);
VSPSIM=zeros(T,1);
N=1;


parfor i=1:T
    for j=1:N
    VSPSIM(i,j)=interp1(squeeze(bgrid(1,:)),squeeze(VSP(BGiSIM(i,j),:,S_index(i))),bSIM(i,j),'linear','extrap');
    end
end

Wf_Gains=((VSPSIM*(1-sigma)*(1-beta)+ones(T,1))./(VSIM*(1-sigma)*(1-beta)+ones(T,1))).^(1/(1-sigma))-ones(T,1);

 [num,tex] = xlsread('targets.xls','Sheet1');
targets=[mean(num(:,5)), std(num(:,9)), mean(num(:,2)), mean(num(:,3)),std(num(:,6)), std(num(:,7)),mean(num(:,4)),std(num(:,8))];


 matrix={'Total debt' , targets(7), momsDE(7), moms(7)  ;
         'Private debt', targets(3), momsDE(3),moms(3) ;
         'Public debt', targets(4), momsDE(4),moms(4) ;
         'Mean spread',targets(1),momsDE(1), moms(1);
         'Volatility debt' , targets(8), momsDE(8), moms(8)  ;
         'Volatility private debt', targets(5), momsDE(5),moms(5) ;
         'Volatility public debt', targets(6), momsDE(6),moms(6) ;
         'Volatility spread',targets(2),momsDE(2), moms(2);
 'Probability of a binding constraint','',mbindDE, mean(bindSIM)*100;
 'Probability of a financial crisis','',mSSDE, mean(SS)*100;
 'Probability of default','',mDefDE, mean(DefSIM)*100;
 'Welfare gains','','', mean(Wf_Gains)*100};
  columnLabels = {'Moments', 'Data','Baseline','Social planner'};

  matrix2latex(matrix, 'Tables\Table_3.tex', 'columnLabels', columnLabels, 'alignment', 'c','format', '%-6.2f');